#ifndef _IO_H
#define _IO_H

#define outb(value,port) \
__asm__ ("outb %%al,%%dx"::"a" (value),"d" (port))

#define inb(port) ({ \
    unsigned char _v; \
    __asm__ volatile ("inb %%dx,%%al":"=a" (_v):"d" (port)); \
    _v; \
})	

#define outb_p(value, port)     \
__asm__("outb %%al, %%dx\n"     \
        "\tjmp 1f\n"            \
        "1:\tjmp 1f\n"          \
        "1:" :: "a"(value), "d"(port))

#define inb_p(port) ({ \
unsigned char _v; \
__asm__ volatile ("inb %%dx,%%al\n" \
    "\tjmp 1f\n" \
    "1:\tjmp 1f\n" \
    "1:":"=a" (_v):"d" (port)); \
_v; \
})

// 读取 eflags 寄存器
#define load_eflags() ({ \
    unsigned long flags; \
    __asm__ volatile ( \
        "pushf\n\t" \
        "pop %0\n\t" \
        : "=g" (flags) \
    ); \
    flags; \
})
/*
// 读取EFLAGS的宏
#define load_eflags() ({          \
    unsigned int _eflags;         \
    __asm__ volatile (            \
        "pushf\n\t"               \
        "pop %0"                  \
        : "=r" (_eflags)          \
    );                            \
    _eflags;                      \
})*/

// 赋值 eflags 寄存器
#define store_eflags(value) ({ \
    __asm__ volatile ( \
        "push %0\n\t" \
        "popf\n\t" \
        : \
        : "g" (value) \
        : "memory" \
    ); \
})

/*
// 设置EFLAGS的宏
#define store_eflags(val) do {    \
    unsigned int _val = (val);    \
    __asm__ volatile (            \
        "push %0\n\t"             \
        "popf"                    \
        :                         \
        : "r" (_val)              \
        : "cc"                   \
    );                            \
} while(0)
*/

extern int io_load_eflags();
extern void io_store_eflags(int eflags);
extern int io_in8(int port);
extern int io_in16(int port);
extern int io_in32(int port);
extern void io_out8(int port, int data);
extern void io_out16(int port, int data);
extern void io_out32(int port, int data);

#endif
